package me.rocky.domain;

import me.rocky.entity.SysUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Rocky
 * @version 1.0
 * @description
 * @email inaho00@foxmail.com
 * @createDate 2021/9/22 23:24
 * @log
 */
public class AuthUser implements UserDetails {
	private static final long serialVersionUID = -4939462222977382961L;
	private String username;
	private String password;
	private  Collection<SimpleGrantedAuthority> authorities;

	public AuthUser(SysUser user){
		this.username = user.getUsername();
		this.password = user.getPassword();
		this.authorities = new ArrayList<SimpleGrantedAuthority>();
		List<SimpleGrantedAuthority> auths = Arrays.stream(user.getAuthories().split(","))
				.map(SimpleGrantedAuthority::new)
				.collect(Collectors.toList());
		List<SimpleGrantedAuthority> roles = Arrays.stream(user.getRoles().split(","))
						.map(SimpleGrantedAuthority::new)
						.collect(Collectors.toList());
		this.authorities.addAll(auths);
		this.authorities.addAll(roles);

	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return this.authorities;
	}

	@Override
	public String getPassword() {
		return this.password;
	}

	@Override
	public String getUsername() {
		return this.username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return false;
	}

	@Override
	public boolean isAccountNonLocked() {
		return false;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return false;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
}
